package cn.kgc.itrip.biz.service.impl;

import cn.kgc.itrip.beans.pojo.ItripHotelRoom;
import cn.kgc.itrip.beans.pojo.ItripImage;
import cn.kgc.itrip.beans.vo.ItripImageVO;
import cn.kgc.itrip.beans.vo.hotelroom.ItripHotelRoomVO;
import cn.kgc.itrip.beans.vo.hotelroom.SearchHotelRoomVO;
import cn.kgc.itrip.biz.service.HotelRoomService;
import cn.kgc.itrip.dao.itriphotelroom.ItripHotelRoomMapper;
import cn.kgc.itrip.dao.itriphoteltempstore.ItripHotelTempStoreMapper;
import cn.kgc.itrip.dao.itripimage.ItripImageMapper;
import cn.kgc.itrip.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class HotelRoomServiceImpl implements HotelRoomService {

    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    private ItripImageMapper itripImageMapper;
    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Override
    public List<List<ItripHotelRoomVO>> queryHotelRoomByHotel(SearchHotelRoomVO vo) throws Exception {
        List<List<ItripHotelRoomVO>> itripHotelRoomVOS = new ArrayList<>();
        Map<String, Object> params = new HashMap<>();
        params.put("hotelId",vo.getHotelId());
        params.put("isBook",vo.getIsBook());
        params.put("isCancel",vo.getIsCancel());
        params.put("isHavingBreakfast",vo.getIsHavingBreakfast());
        params.put("isTimelyResponse",vo.getIsTimelyResponse());
        params.put("payType",vo.getPayType());
        params.put("roomBedTypeId",vo.getRoomBedTypeId());
        // 时间工具类
        List<Date> list = DateUtil.getBetweenDates(vo.getStartDate(), vo.getEndDate());
        params.put("startDate",list.get(0));
        params.put("endDate",list.get(list.size() - 1));
        List<ItripHotelRoom> itripHotelRooms = itripHotelRoomMapper.getItripHotelRoomListWithStore(params);
        for (ItripHotelRoom itripHotelRoom : itripHotelRooms) {
            List<ItripHotelRoomVO> vos = new ArrayList<>();
            ItripHotelRoomVO itripHotelRoomVO = new ItripHotelRoomVO();
            BeanUtils.copyProperties(itripHotelRoom,itripHotelRoomVO);
            vos.add(itripHotelRoomVO);
            itripHotelRoomVOS.add(vos);
        }
        return itripHotelRoomVOS;
    }
    @Override
    public List<ItripImageVO> getImg(Long roomId) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("targetId",roomId);
        params.put("type",1); // 房间图片
        List<ItripImage> itripImages = itripImageMapper.getItripImageListByMap(params);

        // 转换
        List<ItripImageVO> vos = new ArrayList<>();
        for (ItripImage itripImage : itripImages) {
            ItripImageVO vo = new ItripImageVO();
            vo.setImgUrl(itripImage.getImgUrl());
            vo.setPosition(itripImage.getPosition());
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public int getRoomStock(Long roomId, List<Date> betweenDates) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("roomId",roomId);
        // 时间工具类
        params.put("startDate",betweenDates.get(0));
        params.put("endDate",betweenDates.get(betweenDates.size() - 1));
        int stock = itripHotelTempStoreMapper.getItripHotelStore(params);
        return stock;
    }


    @Override
    public boolean checkRoomStock(Long roomId, Integer count, List<Date> betweenDates) throws Exception {
        int roomStock = getRoomStock(roomId, betweenDates);
        return roomStock >= count;
    }

    @Override
    public ItripHotelRoom getRoomById(Long roomId) throws Exception {
        return itripHotelRoomMapper.getItripHotelRoomById(roomId);
    }
}
